<template>
  <div>
    <div class="container">
      <div class="user-info">
        <van-image
          width="64"
          fit="cover"
          round
          height="64"
          :src="userInfo.photo"
        />

        <h2 class="name">
          {{ userInfo.name }}
          <br />
          <van-tag type="primary" color="#fff" text-color="#0098fa">{{
            userInfo.birthday
          }}</van-tag>
        </h2>
      </div>

      <van-row>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#7badff" />
          <br />
          我的作品
        </van-col>
        <van-col span="8">
          <van-icon name="star-o" color="#ff6eb5" />
          <br />
          我的收藏
        </van-col>
        <van-col span="8">
          <van-icon name="tosend" color="#ffbd7c" />
          <br />
          阅读历史
        </van-col>
      </van-row>

      <van-cell-group route>
        <van-cell
          title="编辑资料"
          icon="edit"
          is-link
          @click="$router.push({ path: '/edit' })"
        />
        <van-cell title="小智同学" icon="chat-o" is-link @click="$router.push('/student')"/>
        <van-cell title="系统设置" icon="setting-o" is-link />
        <van-cell title="退出登录" icon="warning-o" is-link @click="logout" />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
// 导入接口
import { getUserInfo } from './user.js'
// 导入token工具包
import { removeToken } from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      userInfo: {}
    }
  },
  created () {
    getUserInfo().then(res => {
      // console.log(res);
      this.userInfo = res.data.data
      // 存储到vuex中
      this.$store.commit('updateUserInfo', res.data.data)
    })
  },
  methods: {
    logout () {
      this.$dialog
        .confirm({
          title: '温馨提示',
          message: '请确认是否退出登录？'
        })
        .then(() => {
          // on confirm
          // 用户点击确认
          // 删除token
          removeToken()
          // 清除用户信息
          this.userInfo = {}
          // 跳转登录页
          this.$router.push('/login')
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="less">
.container {
  .user-info {
    height: 100px;
    background-color: #0098fa;
    display: flex;
    align-items: center;
    padding-left: 15px;

    .name {
      color: #fff;
      font-size: 18px;
      font-weight: 400;
      padding: 0 15px;
    }
  }

  .van-col {
    padding: 15px 0;
    text-align: center;
    font-size: 12px;

    .van-icon {
      margin-bottom: 5px;
      font-size: 24px;
    }
  }
}
</style>
